home *** CD-ROM | disk | FTP | other *** search
/ The Games Machine 165 / XENIATGM165.ISO / cddata / main.dxr / 00010_Loop for X Seconds.ls < prev    next >
Encoding:
Text File  |  2003-05-29  |  6.9 KB  |  176 lines

  1. property myTimeOut, myTimeUnit, myTimeOutFrame, myImmediateJump, myStartFrame, myStartTicks, myEndFrame
  2.  
  3. on getBehaviorDescription me
  4.   return "LOOP FOR X SECONDS: FRAME BEHAVIOR" & RETURN & RETURN & "This behavior will make the playback head loop for a fixed length of time over a certain number of frames, then jump to a chosen marker. " & "You can choose whether the playback head jumps immediately at the end of the period, or whether it should run right through to the last frame of the span." & RETURN & RETURN & "Drag this behavior to the frame channel of the Score Window, then stretch it out over the frames you wish to loop over. " & "If you wish to remain on the same frame, then simply do not stretch out the sprite." & RETURN & RETURN & "PERMITTED MEMBER TYPES:" & RETURN & "Frame behavior" & RETURN & RETURN & "PARAMETERS:" & RETURN & "* Duration of loop (1 tick - 120 hours)" & RETURN & "* Marker to jump to at the end of the period" & RETURN & "* Playback head jumps immediately | at the end of the cycle"
  5. end
  6.  
  7. on getBehaviorTooltip me
  8.   return "Frame behavior. " & "Stretch this behavior over a sequence of frames to make the playback head loop for a fixed length of time then jump to a chosen marker. " & "Option: ensure that the cycle is fully completed before the playback head jumps."
  9. end
  10.  
  11. on beginSprite me
  12.   Initialize(me)
  13. end
  14.  
  15. on exitFrame me
  16.   CheckTimeOut(me)
  17. end
  18.  
  19. on Initialize me
  20.   thisSprite = sprite(the currentSpriteNum)
  21.   myStartFrame = thisSprite.startFrame
  22.   myEndFrame = thisSprite.endFrame
  23.   if symbolp(myTimeOutFrame) then
  24.     case myTimeOutFrame of
  25.       #previous:
  26.         jumpToFrame = marker(-1)
  27.       #loop:
  28.         jumpToFrame = marker(0)
  29.       #next:
  30.         jumpToFrame = marker(1)
  31.     end case
  32.   else
  33.     jumpToFrame = marker(myTimeOutFrame)
  34.   end if
  35.   if the currentSpriteNum then
  36.     ErrorAlert(me, #invalidChannel, the currentSpriteNum)
  37.   end if
  38.   if not jumpToFrame then
  39.     jumpToFrame = myEndFrame + 1
  40.     ErrorAlert(me, #missingMarker, jumpToFrame)
  41.   else
  42.     if (jumpToFrame >= myStartFrame) and (jumpToFrame <= myEndFrame) then
  43.       jumpToFrame = myEndFrame + 1
  44.       ErrorAlert(me, #endlessLoop, jumpToFrame)
  45.     end if
  46.   end if
  47.   myStartTicks = the ticks
  48.   case myTimeUnit of
  49.     "Seconds":
  50.       myTimeOut = myTimeOut * 60
  51.     "Minutes":
  52.       myTimeOut = myTimeOut * 60 * 60
  53.     "Hours":
  54.       myTimeOut = myTimeOut * 60 * 60 * 60
  55.   end case
  56.   myTimeOut = myTimeOut + myStartTicks
  57.   myTimeOutFrame = jumpToFrame
  58.   myImmediateJump = myImmediateJump = "jump immediately"
  59. end
  60.  
  61. on CheckTimeOut me
  62.   if the ticks > myTimeOut then
  63.     if myImmediateJump or (the frame = myEndFrame) then
  64.       go(myTimeOutFrame)
  65.     end if
  66.   else
  67.     if the frame = myEndFrame then
  68.       go(myStartFrame)
  69.     end if
  70.   end if
  71. end
  72.  
  73. on substituteStrings me, parentString, childStringList
  74.   i = childStringList.count()
  75.   repeat while i
  76.     tempString = EMPTY
  77.     dummyString = childStringList.getPropAt(i)
  78.     replacement = childStringList[i]
  79.     lengthAdjust = dummyString.char.count - 1
  80.     repeat while 1
  81.       position = offset(dummyString, parentString)
  82.       if not position then
  83.         parentString = tempString & parentString
  84.         exit repeat
  85.         next repeat
  86.       end if
  87.       if position <> 1 then
  88.         tempString = tempString & parentString.char[1..position - 1]
  89.       end if
  90.       tempString = tempString & replacement
  91.       delete parentString.char[1..position + lengthAdjust]
  92.     end repeat
  93.     i = i - 1
  94.   end repeat
  95.   return parentString
  96. end
  97.  
  98. on ErrorAlert me, theError, data
  99.   behaviorName = string(me)
  100.   delete word 1 of behaviorName
  101.   delete char -30001 of behaviorName
  102.   delete char -30001 of behaviorName
  103.   case theError of
  104.     #invalidChannel:
  105.       if the runMode = "Author" then
  106.         terror1 = "BEHAVIOR ERROR: Frame ^0, Sprite ^1"
  107.         terror1 = substituteStrings(me, terror1, ["^0": the frame, "^1": the currentSpriteNum])
  108.         terror2 = "Behavior ^0 should be attached to the frame script channel."
  109.         terror2 = substituteStrings(me, terror2, ["^0": behaviorName])
  110.         terror3 = "Current Channel = ^0"
  111.         terror3 = substituteStrings(me, terror3, ["^0": data])
  112.         alert(terror1 & RETURN & RETURN & terror2 & RETURN & RETURN & terror3)
  113.         abort()
  114.       end if
  115.     #missingMarker:
  116.       if the runMode = "Author" then
  117.         terror1 = "BEHAVIOR ERROR: Frame ^0, Sprite ^1"
  118.         terror1 = substituteStrings(me, terror1, ["^0": the frame, "^1": the currentSpriteNum])
  119.         terror2 = "Frame behavior ^0 is set to jump to marker ^1. " & "This marker cannot be found. " & " Choose a valid marker in the Behavior Parameters dialog."
  120.         terror2 = substituteStrings(me, terror2, ["^0": behaviorName, "^1": myTimeOutFrame])
  121.         terror3 = "In the meantime, frame ^0 will be used instead."
  122.         terror3 = substituteStrings(me, terror3, ["^0": data])
  123.         alert(terror1 & RETURN & RETURN & terror2 & RETURN & RETURN & terror3)
  124.       end if
  125.     #endlessLoop:
  126.       if the runMode = "Author" then
  127.         if symbolp(myTimeOutFrame) then
  128.           case myTimeOutFrame of
  129.             #previous:
  130.               jumpToFrame = marker(-1)
  131.             #loop:
  132.               jumpToFrame = marker(0)
  133.             #next:
  134.               jumpToFrame = marker(1)
  135.           end case
  136.         else
  137.           jumpToFrame = marker(myTimeOutFrame)
  138.         end if
  139.         terror1 = "BEHAVIOR ERROR: Frame ^0, Sprite ^1"
  140.         terror1 = substituteStrings(me, terror1, ["^0": the frame, "^1": the currentSpriteNum])
  141.         terror2 = "Frame behavior ^0 is set to jump to marker ^1 (frame ^2 ). " & "  This is within the span of the behavior and will cause an endless loop."
  142.         terror2 = substituteStrings(me, terror2, ["^0": behaviorName, "^1": myTimeOutFrame, "^2": jumpToFrame])
  143.         terror3 = "Frame ^0 will be used instead."
  144.         terror3 = substituteStrings(me, terror3, ["^0": data])
  145.         alert(terror1 & RETURN & RETURN & terror2 & RETURN & RETURN & terror3)
  146.       end if
  147.   end case
  148. end
  149.  
  150. on isOKToAttach me, aSpriteType, aSpriteNum
  151.   tisok = 0
  152.   if aSpriteType = #script then
  153.     tisok = 1
  154.   end if
  155.   return tisok
  156. end
  157.  
  158. on getPropertyDescriptionList me
  159.   nextMarker = nextMarker(me)
  160.   return [#myTimeOut: [#comment: "Loop over selected frames for...", #format: #integer, #range: [#min: 1, #max: 120], #default: 30], #myTimeUnit: [#comment: EMPTY, #format: #string, #range: ["Ticks", "Seconds", "Minutes", "Hours"], #default: "Seconds"], #myTimeOutFrame: [#comment: "... " & "then jump to marker", #format: #marker, #default: nextMarker], #myImmediateJump: [#comment: "When the time is up", #format: #string, #range: ["complete the loop", "jump immediately"], #default: "jump immediately"]]
  161. end
  162.  
  163. on nextMarker me
  164.   labelString = the labelList
  165.   delete char -30000 of labelString
  166.   markerCount = the number of lines in labelString
  167.   theFrame = the frame
  168.   repeat with i = 1 to markerCount
  169.     theMarker = line i of labelString
  170.     markerFrame = marker(theMarker)
  171.     if theFrame < markerFrame then
  172.       return theMarker
  173.     end if
  174.   end repeat
  175. end
  176.